package cn.fuxi2022.module.banner.service.impl;

import cn.fuxi2022.core.utils.Query;
import cn.fuxi2022.model.empty.applets.GroupBanner;
import cn.fuxi2022.model.resp.PageUtils;
import cn.fuxi2022.module.banner.mapper.GroupBannerMapper;
import cn.fuxi2022.module.banner.service.GroupBannerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 小程序首页轮播图表 服务实现类
 * </p>
 *
 * @author fuxi
 * @since 2022-10-15 11:06:59
 */
@Service
public class GroupBannerServiceImpl extends ServiceImpl<GroupBannerMapper, GroupBanner> implements GroupBannerService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        // 1.构建搜索条件
        LambdaQueryWrapper<GroupBanner> wrapper = new LambdaQueryWrapper<>();
        // 按照创建日期排序
        wrapper.orderByAsc(GroupBanner::getSort);
        wrapper.orderByDesc(GroupBanner::getGmtCreate);

        // 2.获取请求参数，判断是否进行搜索查询
        // 搜索标题或描述
        if (ObjectUtils.isNotEmpty(params.get("search"))) {
            String search = String.valueOf(params.get("search"));
            wrapper.like(GroupBanner::getTitle,search).or().like(GroupBanner::getDescription,search);
        }

        // 搜索排序
        if (ObjectUtils.isNotEmpty(params.get("sort"))) {
            wrapper.eq(GroupBanner::getSort,params.get("sort"));
        }

        // 3.工具分页
        IPage<GroupBanner> page = this.page(
                new Query<GroupBanner>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }
}
